package com.qinge.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinge.blog.dao.BlogImageDao;
import com.qinge.blog.entity.BlogImage;
import com.qinge.blog.entity.BlogImageExample;
import com.qinge.blog.utils.CurrentUserUtil;
import com.qinge.blog.vo.paging.PagingReqVO;
import org.springframework.stereotype.Service;
import com.qinge.blog.service.BlogImageService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogImage service层实现类
 */
@Service
public class BlogImageServiceImpl   implements BlogImageService {
    @Resource
    BlogImageDao imageDao;

    @Override
    public void add(BlogImage... blogImages) {
        for( BlogImage image:blogImages){
            image.setUserId(CurrentUserUtil.getUserId());
        }

        imageDao.batchInsert(Arrays.asList(blogImages));
    }

    @Override
    public void addByList(List<BlogImage> blogImages) {
        for( BlogImage image:blogImages){
            image.setUserId(CurrentUserUtil.getUserId());
        }

        imageDao.batchInsert(blogImages);
    }

    @Override
    public void removeById(String id) {
        imageDao.deleteByPrimaryKey(id);
    }

    @Override
    public void removeBatchByIds(List<String> ids) {
        BlogImageExample example=new BlogImageExample();
        BlogImageExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        imageDao.deleteByExample(example);
    }

    @Override
    public void update(BlogImage blogImage) {
        imageDao.upsert(blogImage);
    }

    @Override
    public void upsert(BlogImage blogImage) {
        imageDao.upsert(blogImage);
    }

    @Override
    public BlogImage findById(String id) {
        return imageDao.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogImage> findByIds(List<String> ids) {
        BlogImageExample example=new BlogImageExample();
        BlogImageExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        return imageDao.selectByExample(example);
    }

    @Override
    public PageInfo<BlogImage> findAll() {
        return paging(new PagingReqVO<BlogImage>());
    }

    @Override
    public PageInfo<BlogImage> findAll(PagingReqVO<BlogImage> pagingReqVO) {
        return paging(pagingReqVO);
    }

    @Override
    public PageInfo<BlogImage> paging(PagingReqVO<BlogImage> pagingReqVO) {
        PageHelper.startPage(pagingReqVO);

        BlogImageExample example=new BlogImageExample();

        BlogImageExample.Criteria criteria=example.createCriteria();

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        List<BlogImage> list=imageDao.selectByExample(example);

        return new PageInfo<>(list);
    }

}

